const COS_ADDR = process.env.COS_ADDR

export const getBlocks = ({ contentBlock, blocks }) => {
  let t = []
  if (contentBlock && contentBlock.value) {
    contentBlock.type = 'text'
    t.push(contentBlock)
  }
  t = t.concat(blocks)
  return t
}

export const getContentFromBlocks = (blocks) => {
  let content = ''
  for (let i = 0; i < blocks.length; i++) {
    let n = blocks[i]
    if (n.type === 'text') {
      content = n.value
      break
    }
  }
  return content
}

export const getMediaBlocksFromBlocks = (blocks) => {
  let mediaBlocks = []
  for (let i = 0; i < blocks.length; i++) {
    let n = blocks[i]
    if (n.type === 'text') continue
    mediaBlocks.push(n)
  }
  return mediaBlocks
}

export const getUploadQueueFromBlocks = (blocks) => {
  let uploadQueue = []
  let audioCount = 1
  for (let i = 0; i < blocks.length; i++) {
    let n = blocks[i]
    if (n.type === 'text') continue
    if (n.type === 'audio') {
      uploadQueue.push({
        duration: n.meta.duration || 0,
        path: COS_ADDR + n.key,
        size: n.size || 0,
        idx: audioCount++,
        status: 'enable',
        type: 'audio'
      })
    } else if (n.type === 'video') {
      uploadQueue.push({
        path: COS_ADDR + n.key,
        size: n.size || 0,
        status: 'enable',
        type: 'video',
        meta: n.meta,
        thumbTempFilePath: COS_ADDR + n.meta.coverPicKey
      })
    } else if (n.type === 'image') {
      uploadQueue.push({
        path: COS_ADDR + n.key,
        size: n.size || 0,
        status: 'enable',
        type: 'pic'
      })
    }
  }
  return uploadQueue
}

export const getCardPicPathFromExercise = (exercise) => {
  let path = ''
  if (!exercise.exerciseParts) return path
  exercise.exerciseParts.forEach(n => {
    if (!n.resources) return
    n.resources.forEach(nn => {
      if (nn.resourceType !== 'IMAGE') return
      path = nn.url
    })
  })
  return path
}

export const getCardAudioPathFromExercise = (exercise) => {
  let path = ''
  if (!exercise.exerciseParts) return path
  exercise.exerciseParts.forEach(n => {
    if (!n.resources) return
    n.resources.forEach(nn => {
      if (nn.resourceType !== 'AUDIO') return
      path = nn.url
    })
  })
  return path
}

export const getCardTextFromExercise = (exercise) => {
  let text = ''
  if (!exercise.exerciseParts) return text
  exercise.exerciseParts.forEach(n => {
    if (!n.text) return
    text = n.text
  })
  return text
}
